package com.hjm.project.statistical.snAllot.service.impl;

import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.text.Convert;
import com.hjm.project.agentInfo.agentInfo.domain.AgentInfo;
import com.hjm.project.agentInfo.agentInfo.mapper.AgentInfoMapper;
import com.hjm.project.statistical.snAllot.domain.SnAllot;
import com.hjm.project.statistical.snAllot.mapper.SnAllotMapper;
import com.hjm.project.statistical.snAllot.service.ISnAllotService;
import com.hjm.project.system.sysConfigJob.domain.SysConfigJob;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 终端分配统计Service业务层处理
 * 
 * @author maker-platform
 * @date 2022-01-17
 */
@Service
public class SnAllotServiceImpl implements ISnAllotService {
    @Resource
    private SnAllotMapper snAllotMapper;

    @Resource
    private AgentInfoMapper agentInfoMapper;

    /**
     * 查询终端分配统计
     * 
     * @param id 终端分配统计ID
     * @return 终端分配统计
     */
    @Override
    public SnAllot selectSnAllotById(Integer id) {
        return snAllotMapper.selectSnAllotById(id);
    }

    /**
     * 查询终端分配统计列表
     * 
     * @param snAllot 终端分配统计
     * @return 终端分配统计
     */
    @Override
    public List<SnAllot> selectSnAllotList(SnAllot snAllot) {
        return snAllotMapper.selectSnAllotList(snAllot);
    }

    /**
     * 查询终端分配统计列表
     *
     * @param snAllot 终端分配统计
     * @return 终端分配统计 一个结果
     */
    @Override
    public SnAllot selectSnAllotOne(SnAllot snAllot) {
        return snAllotMapper.selectSnAllotOne(snAllot);
    }

    /**
     * 新增终端分配统计
     * 
     * @param snAllot 终端分配统计
     * @return 结果
     */
    @Override
    public int insertSnAllot(SnAllot snAllot) {
        return snAllotMapper.insertSnAllot(snAllot);
    }

    /**
     * 修改终端分配统计
     * 
     * @param snAllot 终端分配统计
     * @return 结果
     */
    @Override
    public int updateSnAllot(SnAllot snAllot) {
        return snAllotMapper.updateSnAllot(snAllot);
    }

    /**
     * 删除终端分配统计对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSnAllotByIds(String ids) {
        return snAllotMapper.deleteSnAllotByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除终端分配统计信息
     * 
     * @param id 终端分配统计ID
     * @return 结果
     */
    @Override
    public int deleteSnAllotById(Integer id) {
        return snAllotMapper.deleteSnAllotById(id);
    }


    @Override
    public void snAllotCount(SysConfigJob configJob) throws Exception {
        try{
            SnAllot snAllot = new SnAllot();
            if (configJob.getConfigId() != null) {
                snAllot.setCreateDate(DateUtils.getDay(DateUtils.YYYY_MM_DD,1,configJob.getConfigValue()));
                snAllot.setMonth(DateUtils.getDay(DateUtils.YYYY_MM,0,configJob.getConfigValue()));
            } else {
                snAllot.setCreateDate(DateUtils.getDay(DateUtils.YYYY_MM_DD,1,DateUtils.getDate()));
                snAllot.setMonth(DateUtils.getDateYYYY_MM());
            }

            //清除旧数据
            snAllotMapper.delSnAllotCount(snAllot.getCreateDate());

            //获取所有代理的终端使用情况
            List<AgentInfo> allAgentList = agentInfoMapper.selectAgentInfoList(null);
            for (AgentInfo agentInfo : allAgentList) {
                //获取当前代理本级及以下终端数据
                //snAllotMapper.selectSnAllotOne()







            }



         /*   List<SnCountData> list = snCountDataMapper.suCountDataSum(snCountData);
            if (CollectionUtils.isNotEmpty(list)) {
                int i = snCountDataMapper.insertSnCountDataBatch(list);
                if (i <= 0) {
                    throw new BusinessException("统计结果新增失败,更新结果:" + i);
                }
            } else {
                throw new BusinessException("统计结果集为空:" + list.size());
            }*/
        }catch (Exception e){
            throw new BusinessException("统计异常：" + StringUtils.substring(e.getMessage(), 0, 2000));
        }
    }

}
